package androidchat.com.view;

import java.io.IOException;
import java.security.acl.Group;

import com.androidchat.R;
import com.androidchat.R.layout;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import androidchat.com.common.MessageType;
import androidchat.com.model.ChatConnectThread;
import androidchat.com.model.ClientChat;
import androidchat.com.model.ClientConnectServerThread;
import androidchat.com.model.ReceiveMessage;
import androidchat.com.model.SendMessage;
import androidchat.com.sqlitedb.SqliteDB;
import androidchat.com.view.ManageFragment;

public class MainActivity extends Activity {
     RadioGroup rag;
     FragmentManager fragmentManager;  
     public static String account,nick;
     private View menu_layout;
 	private PopupWindow pop;
 	private int menu_state=0;
 	 private Button exit;
 	Fragment  frgs[] =new Fragment[4];
 	FragmentTransaction transaction,transaction2;
    public static Context context;
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainactivity);
		context=getApplicationContext();
		fragmentManager = getFragmentManager();  
		rag=(RadioGroup)findViewById(R.id.radiogroup);
		transaction= fragmentManager.beginTransaction();  
		frgs[0]=new Fragment_huihua();
		frgs[1]=new FriendFragment();
		frgs[2]=new GroupFragment();
		frgs[3]=new Fragment_setting();
		transaction.replace(R.id.content, frgs[1]).commit();
		new SqliteDB(MainActivity.this).getWritableDatabase();
		rag.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@SuppressLint("NewApi")
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				transaction2=fragmentManager.beginTransaction(); 
				
	                switch (checkedId) {  
	                case R.id.tab_recent:  
	                   transaction2.replace(R.id.content, frgs[0]).commit();              
	                    break;  
	                case R.id.tab_frend:  
	                	transaction2.replace(R.id.content, frgs[1]).commit();   
	                    break;  
	                case R.id.tab_group:  
	                	transaction2.replace(R.id.content, frgs[2]).commit();   
	                    break;  
	                case R.id.tab_more:
	                	transaction2.replace(R.id.content, frgs[3]).commit();   
		                break;  
	                default:  
	                    break;  
	                }  
				
		}
		});
		
		
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event){
		if(keyCode==KeyEvent.KEYCODE_MENU){
			if(menu_state==1) return false;
			else{
				menu_layout =getLayoutInflater().inflate(R.layout.menu, null); 
				pop=new PopupWindow(menu_layout,getWindowManager() 
						.getDefaultDisplay().getWidth(), getWindowManager() 
						.getDefaultDisplay().getHeight());
				pop.showAtLocation(menu_layout, Gravity.BOTTOM, 0, 0);
				menu_state=1;
				exit=(Button)menu_layout.findViewById(R.id.exit);
				Animation an=AnimationUtils.loadAnimation(this, R.anim.menu_exit);
				exit.startAnimation(an);
				exit.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						//new SendMessage().sendManageInfo(account,MessageType.OFFLINE,"");
						Log.i("off line ", "ok!");
						//finish();
						System.exit(0);
						ReceiveMessage.b=false;
						ClientConnectServerThread.a=false;
						try {
							ClientChat.sc.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						ClientChat.client.close();
						
					}
					
				});
			}
		}
		if(keyCode==KeyEvent.KEYCODE_BACK){
			if(menu_state==1){
				Animation no=AnimationUtils.loadAnimation(this, R.anim.menu_exit_no);
				exit.startAnimation(no);
				no.setAnimationListener(new AnimationListener(){

					@Override
					public void onAnimationEnd(Animation arg0) {
						// TODO Auto-generated method stub
						pop.dismiss();
						menu_state=0;
					}

					@Override
					public void onAnimationRepeat(Animation arg0) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onAnimationStart(Animation arg0) {
						// TODO Auto-generated method stub
						
					}
					
				});
				
			}
			else{
				moveTaskToBack(true);
			}
		}
		
		return false;
		
	}
	

}
